package com.azhoucode.service.impl;

import com.azhoucode.constant.RedisConstant;
import com.azhoucode.entity.*;
import com.azhoucode.enums.DBType;
import com.azhoucode.service.DBService;
import com.azhoucode.util.*;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.sql.Connection;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: db
 * @author: azhou
 * @create: 2021-11-01 14:54
 **/
@Service
public class DBServiceImpl implements DBService {


    private List<String> paramList = Arrays.asList("id", "create_time", "update_time", "create_id", "update_id");

    @Override
    public String checkDBParam(ApplicationEntity applicationEntity) throws Exception {
        Connection connection = DBUtils.getConnection(applicationEntity);
        if (connection == null) throw new Exception("连接失败");
        String random = RandomUtil.getRamdom();
        RedisCacheUtils.set(String.format(RedisConstant.DB_PARAM, random), applicationEntity, 60 * 60 * 24);
        return random;
    }

    @Override
    public TableEntity getTableColList(String tableName, String random) throws Exception {
        ApplicationEntity applicationEntity = RedisCacheUtils.get(String.format(RedisConstant.DB_PARAM, random), ApplicationEntity.class);
        if (applicationEntity == null) throw new Exception("数据库连接异常");
        Connection connection = DBUtils.getConnection(applicationEntity);
        List<TableNameAndType> columnNameAndTypes = DBUtils.getColumnNameAndTypes(DBType.MYSQL.getValue(), tableName, connection);
        if (CollectionUtils.isEmpty(columnNameAndTypes)) return null;
        TableEntity tableEntity = new TableEntity();
        // 组装表的列数据
        List<TableColEntity> collect = columnNameAndTypes.stream().map(item -> getTableColEntity(item)).collect(Collectors.toList());
        tableEntity.setTableColEntityList(collect);
        return tableEntity;
    }

    @Override
    public List<String> getAllTable(String random) throws Exception {
        ApplicationEntity applicationEntity = RedisCacheUtils.get(String.format(RedisConstant.DB_PARAM, random), ApplicationEntity.class);
        if (applicationEntity == null) throw new Exception("表格基本数据异常");
        String tableName = applicationEntity.getTableName();
        if (tableName.startsWith("#")) tableName = tableName.substring(1);
        if (tableName.endsWith("#")) tableName = tableName.substring(0, tableName.length() - 1);
        List<String> list = null;
        try {
            list = Arrays.asList(tableName.split("#"));
        } catch (Exception e) {
            throw new Exception("表格基本数据异常");
        }
        return list;
    }


    public TableColEntity getTableColEntity(TableNameAndType tableNameAndType) {
        TableColEntity tableColEntity = new TableColEntity();
        tableColEntity.setColName(tableNameAndType.getColName());
        tableColEntity.setChinaName(tableNameAndType.getComment());
        // 如果是ID则默认不显示  不修改  不添加
        String colName = tableColEntity.getColName();
        boolean flag = checkParam(colName);
        tableColEntity.setTableShow(flag ? false : true);
        tableColEntity.setUpdateShow(flag ? false : true);
        tableColEntity.setSearchShow(false);
        tableColEntity.setCompareType("=");
        tableColEntity.setShowType("text");
        tableColEntity.setUpdateType("text");
        tableColEntity.setUploadImg(false);
        tableColEntity.setParamType(tableNameAndType.getEntityParamType());
        return tableColEntity;
    }

    public boolean checkParam(String param) {
        return paramList.contains(param);
    }

}
